"""
 -*- coding: utf-8-*-
 @File: page_base.py
 @Author: chen
 @time: 2023/09/03/ 15:12
"""
from selenium import webdriver
from selenium.webdriver import ActionChains, Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions
from selenium.webdriver.support.wait import WebDriverWait

from stage_L1.lim_project.page_po.utils.mgt_exception import record_exception
from stage_L1.lim_project.page_po.utils.mgt_logger import logger


class BasePage:
    def __init__(self, base_driver=None):
        if base_driver:
            self.driver = base_driver
        else:
            self.driver = webdriver.Chrome()
        self.BY = By
        self.driver.maximize_window()
        self.driver.implicitly_wait(5)
        self.action = ActionChains(self.driver)

    def do_get(self, url):
        self.driver.get(url)
        logger.info(f"打开网站：{url}--->成功")

    def do_find(self, locator: tuple):
        """获取单个元素"""
        try:
            element = self.driver.find_element(*locator)
        except Exception as e:
            logger.error(f"查找元素{locator}--->失败")
            logger.error(e)
            return None
        else:
            logger.info(f"查找元素{locator}--->成功")
            return element

    # @record_exception
    def do_finds(self, locator: tuple, i=0):
        """获取多个元素，默认返回第一个元素"""
        elements = self.driver.find_elements(*locator)
        if elements:
            logger.info(f"查找元素{locator}--->成功")
            return elements[i]
        else:
            logger.info(f"查找元素{locator}--->失败")
            return None

    @record_exception
    def do_click(self, locator: tuple):
        """点击操作"""
        self.wait_until_clickable(locator)
        ele = self.do_find(locator)
        try:
            ele.click()
            logger.info(f"点击元素{locator}--->成功")
        except Exception as err:
            logger.info(f"点击元素{locator}--->失败")
            logger.info(f"异常信息：{err}")
            raise

    @record_exception
    def do_input(self, locator: tuple, content):
        """输入操作"""
        self.wait_until_visibility(locator)
        ele = self.do_find(locator)
        ele.send_keys(Keys.CONTROL, "A")
        logger.info("选中输入框--->成功")
        ele.send_keys(Keys.DELETE)
        logger.info("清空输入框--->成功")
        try:
            ele.send_keys(content)
            logger.info(f"元素{locator}输入：{content}--->成功")
        except Exception as err:
            logger.info(f"元素{locator}输入：{content}--->失败")
            logger.info(f"异常信息：{err}")

    def do_quit(self):
        self.driver.quit()
        logger.info("退出浏览器--->成功")

    # @record_exception
    def wait_until_visibility(self, locator: tuple):
        if WebDriverWait(self.driver, 10).until(
                expected_conditions.visibility_of_element_located(locator)
        ):
            logger.info(f"元素{locator}--->可见")
            return True
        else:
            logger.info(f"元素{locator}--->不可见")
            return False

    # @record_exception
    def wait_until_clickable(self, locator: tuple):
        obj = WebDriverWait(self.driver, 10).until(
            expected_conditions.element_to_be_clickable(locator)
        )
        if obj:
            # logger.info(locator, obj)
            logger.info(f"元素{locator}--->可点击")
            return True
        else:
            logger.info(f"元素{locator}--->不可点击")
            return False
